package com.atguigu.gmall.receiver;

import com.atguigu.gmall.constant.RedisConst;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisMessageReceiver {

    @Autowired
    private RedisTemplate redisTemplate;
    //接收消息的方法
    public void receiveChannelMessage(String message){
//        log.info("收到了redis发布订阅消息:{}",message);
        if(!StringUtils.isEmpty(message)) {
            //去掉双引号""23:1""
            message = message.replaceAll("\"","");
            String[] splitMessage = StringUtils.split(message, ":");
            if (splitMessage == null || splitMessage.length == 2) {
                //splitMessage[0]商品Id splitMessage[1]商品状态
                redisTemplate.opsForValue().set(RedisConst.SECKILL_STATE_PREFIX + splitMessage[0],splitMessage[1]);
            }
        }
    }


}
